package pack

import (
	jsoniter "github.com/json-iterator/go"
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_product"
)

func GetProductList(list []*model.Product, categoryMap map[int64]*model.Category, productItemList []*model.ProductItem) []*admin_product.Product {
	var data = make([]*admin_product.Product, 0)
	var productItemMap = make(map[int64][]*model.ProductItem)
	for _, item := range productItemList {
		productItemMap[item.ProductID] = append(productItemMap[item.ProductID], item)
	}
	if len(list) == 0 {
		return data
	}
	for _, item := range list {
		if tmp := Product(item, categoryMap, productItemMap[item.ID]); tmp != nil {
			data = append(data, tmp)
		}
	}
	return data
}

func Product(data *model.Product, categoryMap map[int64]*model.Category, productItemList []*model.ProductItem) *admin_product.Product {
	var info = admin_product.NewProduct()
	if data == nil {
		return info
	}
	info.ID = data.ID
	info.Name = data.Name
	if category, ok := categoryMap[data.CategoryID]; ok {
		info.CategoryName = category.Name
	}
	info.CategoryID = data.CategoryID
	info.SliderImage = data.SliderImage
	info.ShortInfo = data.ShortInfo
	info.UnitName = data.UnitName
	info.Sort = data.Sort
	info.Status = data.Status
	info.Description = data.Description
	info.Image = GetImage(data.Image)
	info.SpecType = data.SpecType
	if !data.CreatedAt.IsZero() {
		info.CreateTime = data.CreatedAt.UnixMilli()
	}
	var price float64
	for _, item := range productItemList {
		if price == 0 || price > item.SellPrice {
			price = item.SellPrice
		}
	}
	info.Price = price
	return info
}

func ProductInfo(data *model.Product, productSpecList []*model.ProductSpec, productSpecValueList []*model.ProductSpecValue) *model.ProductInfo {
	var info = &model.ProductInfo{}
	if data == nil {
		return info
	}
	info.ID = data.ID
	info.Name = data.Name
	info.CategoryID = data.CategoryID
	var sliderImage []string
	if err := jsoniter.UnmarshalFromString(data.SliderImage, &sliderImage); err == nil {
		var sliderImageArray = make([]string, 0)
		for _, s := range sliderImage {
			sliderImageArray = append(sliderImageArray, GetImage(s))
		}
		info.SliderImage = sliderImageArray
	}

	var productSpecValueMap = make(map[int64][]string)
	for _, value := range productSpecValueList {
		productSpecValueMap[value.ProductSpecID] = append(productSpecValueMap[value.ProductSpecID], value.Name)
	}
	var attrItem []model.AttrItem
	for _, spec := range productSpecList {
		attrItem = append(attrItem, model.AttrItem{
			Detail: productSpecValueMap[spec.ID],
			Value:  spec.Name,
		})
	}
	info.AttrsItem = attrItem

	info.ShortInfo = data.ShortInfo
	info.UnitName = data.UnitName
	info.Sort = data.Sort
	info.Status = data.Status
	info.Description = data.Description
	info.Image = GetImage(data.Image)
	info.SpecType = data.SpecType
	info.Keyword = data.Keyword
	var specifications = make([]map[string]interface{}, 0)
	if err := jsoniter.UnmarshalFromString(data.Specifications, &specifications); err == nil {
		for k, v := range specifications {
			if vv, ok := v[PicColumn]; ok {
				specifications[k][PicColumn] = GetImage(vv.(string))
			}
		}
		info.Attrs = specifications
	}
	return info
}
